package org.gl.customview.search;

import java.util.HashSet;
import java.util.Iterator;

import org.gl.android.utils.SharePreferencesUtil;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.widget.Toast;

import com.chanceit.ssss.activity.R;

public class MultiTypeSpinnerAutoComTextSearch {
	Spinner mSpinner;
	public View view;
	private AutoCompleteTextView mAutoCompleteTextView;
	SearchAssistDirctionary dirctionary;
	Context conntext;
	private String[] m;
	Search<?> mSearch;

	public MultiTypeSpinnerAutoComTextSearch(ViewGroup viewGroup,
			Context _conntext, SearchAssistDirctionary _dictionary,
			Search<?> mSearch) {
		LayoutInflater li = (LayoutInflater) _conntext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.conntext = _conntext;
		this.view = li.inflate(R.layout.search, null);
		viewGroup.addView(view, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		this.dirctionary = _dictionary;
		this.mSearch = mSearch;
		initSerchSpinner();
		initAutoComText();
		view.findViewById(R.id.search_imageButton).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						goSearch(mAutoCompleteTextView.getText(), 0);

					}
				});
		// mSearch.search(currentType.hashCode(), "");//初始化一个空白的搜索
	}

	private void initSerchSpinner() {
		mSpinner = (Spinner) view.findViewById(R.id.search_spinner);
		// mSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		if (dirctionary != null && dirctionary.size() > 0) {
			m = new String[dirctionary.size()];
			Iterator<String> mm = dirctionary.keySet().iterator();
			int i = 0;
			while (mm.hasNext()) {
				m[i] = mm.next();
				i++;
			}

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(conntext,
					R.layout.spinner_item_check_text, m);

			// 设置下拉列表的风格
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			// 将adapter 添加到spinner中
			mSpinner.setAdapter(adapter);
			// 添加事件Spinner事件监听
			mSpinner.setOnItemSelectedListener(new SpinnerSelectedListener());
			currentType = m[0];
			mSpinner.setSelection(0, true);
		} else {
			mSpinner.setVisibility(View.GONE);
		}

	}

	private String currentType;
	private HashSet<String> currentDirc;
	private ArrayAdapter<String> av;

	class SpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			currentType = m[arg2];
			currentDirc = dirctionary.get(currentType);
			// ((TextView) arg1.findViewById(android.R.id.text1))
			// .setTextColor(0xFF3395C9);
			// ((TextView) arg1.findViewById(android.R.id.text1))
			// .setTextColor(0xFFf0f0f0);
			av = newstanceArrayAdapter();
			if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				mAutoCompleteTextView.setAdapter(av);
				Toast.makeText(conntext, m[arg2], Toast.LENGTH_SHORT).show();
				goSearch(mAutoCompleteTextView.getText(), 0);
			} else {

			}

		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	Handler handler = new Handler();

	private ArrayAdapter<String> newstanceArrayAdapter() {
		
		int size = currentDirc.size();
		int index = 0;
		Iterator<String> it = currentDirc.iterator();
		String[] array = new String[currentDirc.size()];
		while (index < size) {
			array[index++] = it.next();
		}
		// String[] s = (String[]) currentDirc.toArray();
		return new ArrayAdapter<String>(conntext,
				android.R.layout.simple_dropdown_item_1line, array);
	}

	private void initAutoComText() {
		mAutoCompleteTextView = (AutoCompleteTextView) view
				.findViewById(R.id.search_autoCompleteTextView1);
		if (dirctionary != null && dirctionary.size() > 0) {
			currentDirc = dirctionary.get(currentType);
			av = newstanceArrayAdapter();
			mAutoCompleteTextView.setAdapter(av);
		}
		mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(final Editable s) {
				goSearch(s);
			}

		});
	}

	private void goSearch(final Editable editable) {
		if (mSearch != null && editable != null && !TextUtils.isEmpty(editable)) {
			handler.removeCallbacksAndMessages(null);
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					mSearch.search(currentType.hashCode(), editable.toString());
				}
			}, 2000);

		}
	}

	private void goSearch(final Editable editable, long dely) {
		if (mSearch != null && editable != null && !TextUtils.isEmpty(editable)) {
			handler.removeCallbacksAndMessages(null);
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {
					dirctionary.get(currentType).add(editable.toString());//TODO 存储用户已输入的结果
					SharePreferencesUtil.AddStringSet(currentType, currentDirc);
					mSearch.search(currentType.hashCode(), editable.toString());
				}
			}, dely);

		}
	}
}
